home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: usinternet.com!not-for-mail
- From: Scott Jibben <sjibben@usinternet.com>
- Subject: Re: embedded strtok() calls on different strings?
- Message-ID: <311A8D15.6B5A@usinternet.com>
- Date: Thu, 08 Feb 1996 17:53:57 -0600
- Organization: Jibben Software
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
- MIME-Version: 1.0
- References: <DMGrn7.1Bx0@CompStar.bnr.ca>
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- tzinck@bnr.ca wrote:
- >
- > Hello.
- >
- > I want to do the following:
- >
- > char *string1="This is string one";
- > char *string2="This is string two";
- >
- > void *vp1;
- > void *vp2;
- >
- > vp1 = strtok(string1," ");
- >
- > while (vp1 != NULL){
- > printf("Tok1 = %s\n", (char *) vp1);
- >
- > vp2 = strtok(string2," ");
- > while (vp2!=NULL){
- > printf("Tok2 = %s\n", (char *) vp2);
- > vp2 = strtok(NULL, " ");
- > }
- >
- > vp1 = strtok(NULL, " ");
- > }
- >
- > But the output is :
- > Tok1 = This
- > Tok2 = This
- > Tok2 = is
- > Tok2 = string
- > Tok2 = two
- >
- > as vp1 gets corrupted. Any thoughts ?
-
- strtok() will usually have a static char * in it to do it's work (keep track of where it
- was) when the NULL is passed as the first parm. I've often wondered how re-entrant
- strtok() for OS/2 or Win32, that is, if two or more threads use strtok() at a time will
- it barf? I'd guess that they do, but you never know...
-
- I've written my own versions of strtok() that are re-entrant (no statics or globals).
- It's pretty simple. I have the first parm as a char** and I pass back the address of
- the string after the next token in the string using that parm.
-
- Although I don't have the code here, here's a quick (may be buggy and probably not
- pretty <g>) example:
-
- char * MyStrTok( char **ppsz, char *pszTok )
- {
- char * pszRet;
- char * psz;
- if ( NULL == *ppsz )
- return NULL;
- pszRet = *ppsz;
- psz = strstr( *ppsz, pszTok );
- if ( NULL == psz )
- {
- *ppsz = NULL;
- }
- else
- {
- *ppsz = psz + strlen( pszTok );
- *psz = '\x0';
- }
- return pszRet;
- }
-
- Hope that is enough to get you started. Step through the above code to verify that it
- works. I just typed it in here on the fly.
-
- --
- Scott Jibben, Jibben Software
- Galactic Overlord & Mines of Gorr BBS Door Games
- -----------------------------
- [EMAIL] sjibben@usinternet.com
- [WWW] http://www.usinternet.com/jsw
- [FTP] ftp.europa.com /outgoing/DOORS/jibben
- [FTP] ftp.cts.com /pub/dferber/jibben
- [FIDO] 1:282/115 [BBS] 612-379-8272 (10 USR 28.8 Couriers)
- [VOICE] 612-757-5626 [FAX] 612-757-8687
-
-